//
//  LiuLoginViewController.swift
//  LiuWeiBo
//
//  Created by 刘晓晨 on 2017/3/27.
//  Copyright © 2017年 刘晓晨. All rights reserved.
//

import UIKit
import SVProgressHUD
import YYModel



class LiuLoginViewController: UIViewController {

    //webView
    lazy var webView = UIWebView()
    
   
    //自定义view
    override func loadView() {
        
        view = webView
        webView.delegate = self
        
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.groupTableViewBackground
        setupNavUI()
    }
    
    func setupNavUI() {
        navigationItem.leftBarButtonItem = UIBarButtonItem(titleStr: "取消", andTarget: self, action: #selector(cancelButtonAction))
        navigationItem.rightBarButtonItem = UIBarButtonItem(titleStr: "自动填充", andTarget: self, action: #selector(autoPadding))
        
        requestData()
    }
    
    //点击事件方法实现
    @objc private func cancelButtonAction() {
        dismiss(animated: true, completion: nil)
    }
    
    @objc private func autoPadding() {
        webView.stringByEvaluatingJavaScript(from: "document.getElementById('passwd').focus();document.getElementById('userId').value = '15032215789';document.getElementById('passwd').value = 'lxc851035'")
    }
    
    //请求网络数据
    private func requestData() {
        
        //url
        let dataUrl = "https://api.weibo.com/oauth2/authorize?client_id=2950836797&redirect_uri=http://www.itcast.cn"
        
        //请求对象
        let urlRequest = URLRequest(url: URL(string: dataUrl)!)
        
        //给webView赋值
        webView.loadRequest(urlRequest)
        
        
        
        
        
    }
    
    

}


extension LiuLoginViewController:UIWebViewDelegate {
    
    //开始加载
    func webViewDidStartLoad(_ webView: UIWebView) {
        SVProgressHUD.show()
    }
    
    //加载完成
    func webViewDidFinishLoad(_ webView: UIWebView) {
        SVProgressHUD.dismiss()
    }
    
    //加载失败
    func webView(_ webView: UIWebView, didFailLoadWithError error: Error) {
        SVProgressHUD.dismiss()
    }
    
    //啊将要加载网络请求
    func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {
        
        guard let urlStr = request.url?.absoluteString else {
            return false
        }
        
        if !urlStr.hasPrefix(redirect_Url) {
            //不是我们的地址
            return true
        }
        
        if let query = request.url?.query,query.hasPrefix("code=") {
            let appkey = query.substring(from: "code=".endIndex)
            //请求token
            LiuUserAccountViewModel.sharedUserAccountViewModel.requestToken(code: appkey, finishedCallBack: { (isSuccess) in
                if isSuccess {
                    self.dismiss(animated: false, completion: {
                        //发送通知
                        NotificationCenter.default.post(name: NSNotification.Name(notificationName), object: self)
                    })
                   
                } else {
                    print("登录失败")
                }
            })
            
            
        } else {
            dismiss(animated: true, completion: nil)
        }
        
        return false
        
        
    }
    
}
